package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendWhitelistDAO;
import org.jsola.hr.dto.AttendWhitelistAddDTO;
import org.jsola.hr.dto.AttendWhitelistUpdateDTO;
import org.jsola.hr.entity.AttendWhitelistDO;
import org.jsola.hr.query.AttendWhitelistQuery;
import org.jsola.hr.service.IAttendWhitelistService;
import org.jsola.hr.vo.AttendWhitelistListVO;
import org.jsola.hr.vo.AttendWhitelistVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrAttendWhitelistServiceImpl")
public class AttendWhitelistServiceImpl implements IAttendWhitelistService {

    @Autowired
    private IAttendWhitelistDAO attendWhitelistDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendWhitelistVO save(AttendWhitelistAddDTO attendWhitelistAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendWhitelistDO attendWhitelistDO = attendWhitelistAddDTO.to(AttendWhitelistDO.class);
        // 保存
        attendWhitelistDO = save(attendWhitelistDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return attendWhitelistDO.to(AttendWhitelistVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendWhitelistUpdateDTO attendWhitelistUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendWhitelistDO attendWhitelistDO = attendWhitelistUpdateDTO.to(AttendWhitelistDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendWhitelistDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendWhitelistIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendWhitelistIds);
    }


    @Override
    public AttendWhitelistVO selectById(String attendWhitelistId, String siteId) {
        AttendWhitelistDO attendWhitelistDO = selectDOById(attendWhitelistId, siteId);
        if (attendWhitelistDO == null) {
            return null;
        }
        return attendWhitelistDO.to(AttendWhitelistVO.class);
    }

    @Override
    public List<AttendWhitelistListVO> select(AttendWhitelistQuery attendWhitelistQuery, String siteId) {
        List<AttendWhitelistDO> attendWhitelistDOList = selectDO(attendWhitelistQuery, siteId);
        if(CollectionUtils.isEmpty(attendWhitelistDOList)) {
            return attendWhitelistDOList == null ? null : new ArrayList<>();
        }
        return attendWhitelistDOList.stream()
                .map(attendWhitelistDO -> attendWhitelistDO.to(AttendWhitelistListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendWhitelistQuery attendWhitelistQuery, String siteId) {
        Example example = buildExample(attendWhitelistQuery,siteId);
        return attendWhitelistDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendWhitelistListVO> selectPage(AttendWhitelistQuery attendWhitelistQuery, String siteId) {
        Example example = buildExample(attendWhitelistQuery,siteId);
        Page<AttendWhitelistDO> page = attendWhitelistDAO.selectPageByExample(example,
                attendWhitelistQuery.getPageNo(),
                attendWhitelistQuery.getPageSize());

        return page.to(AttendWhitelistListVO.class);
    }

    @Override
    public AttendWhitelistDO selectDOById(String attendWhitelistId, String siteId) {
        return listById(attendWhitelistId, siteId);
    }

    @Override
    public List<AttendWhitelistDO> selectDO(AttendWhitelistQuery attendWhitelistQuery, String siteId) {
        Example example = buildExample(attendWhitelistQuery,siteId);
        return attendWhitelistDAO.selectByExample(example);
    }

    @Override
    public int deleteByAttendPlanId(String attendPlanId, String siteId) {
        AttendWhitelistQuery attendWhitelistQuery = new AttendWhitelistQuery();
        attendWhitelistQuery.setAttendPlanId(attendPlanId);
        Example example = buildExample(attendWhitelistQuery,siteId);
        return attendWhitelistDAO.deleteByExample(example);
    }

    @Override
    public Boolean isWhitelist(String attendPlanId, String empInfoId, String siteId) {

        AttendWhitelistQuery attendWhitelistQuery = new AttendWhitelistQuery();
        attendWhitelistQuery.setAttendPlanId(attendPlanId);
        attendWhitelistQuery.setEmpInfoId(empInfoId);
        List<AttendWhitelistDO> attendWhitelistDOList = this.selectDO(attendWhitelistQuery, siteId);
        return attendWhitelistDOList.size() > 0;
    }

    @Override
    public Map<String, Boolean> isWhitelist(String attendPlanId, List<String> empInfoIdList, String siteId){
        Map<String, Boolean> result = new HashMap<>(32);
        Example example = new Example(AttendWhitelistDO.class);
        example.and()
                .andEqualTo("attendPlanId", attendPlanId)
                .andIn("empInfoId",empInfoIdList)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        List<AttendWhitelistDO> attendWhitelistList = attendWhitelistDAO.selectByExample(example);
        Map<String, List<AttendWhitelistDO>> group = attendWhitelistList.stream()
                .collect(Collectors.groupingBy(AttendWhitelistDO::getEmpInfoId));
        for (String empId : empInfoIdList) {
            List<AttendWhitelistDO> attendWhitelist = group.get(empId);
            result.put(empId, !CollectionUtils.isEmpty(attendWhitelist));
        }
        return result;
    }

    @Override
    public int logicDelByAttendPlanId(String attendPlanId, TokenUser tokenUser) {

        // 查询
        AttendWhitelistQuery attendWhitelistQuery = new AttendWhitelistQuery();
        attendWhitelistQuery.setAttendPlanId(attendPlanId);
        List<AttendWhitelistDO> dataList = this.selectDO(attendWhitelistQuery, tokenUser.getSiteId());
        if (dataList.size() <= 0) {
            return 0;
        }

        for (AttendWhitelistDO attendWhitelist : dataList) {

            AttendWhitelistDO attendWhitelistDO = new AttendWhitelistDO();
            attendWhitelistDO.setId(attendWhitelist.getId());
            attendWhitelistDO.setValid(false);
            attendWhitelistDO.setModifiedUserId(tokenUser.getUserId());
            attendWhitelistDO.setGmtModified(new Date());
            attendWhitelistDAO.updateByPrimaryKeySelective(attendWhitelistDO);
        }

        return 0;
    }


    /**
     * 根据查询参数，构建example

     * @param attendWhitelistQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(AttendWhitelistQuery attendWhitelistQuery, String siteId) {
        Example example = new Example(AttendWhitelistDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (attendWhitelistQuery.getCompanyId() != null) {
            example.and().andLike("companyId", "%" + attendWhitelistQuery.getCompanyId() + "%");
        }
        if (attendWhitelistQuery.getCompanyName() != null) {
            example.and().andLike("companyName", "%" + attendWhitelistQuery.getCompanyName() + "%");
        }
        if (attendWhitelistQuery.getDeptId() != null) {
            example.and().andLike("deptId", "%" + attendWhitelistQuery.getDeptId() + "%");
        }
        if (attendWhitelistQuery.getDeptName() != null) {
            example.and().andLike("deptName", "%" + attendWhitelistQuery.getDeptName() + "%");
        }
        if (attendWhitelistQuery.getEmpInfoId() != null) {
            example.and().andLike("empInfoId", "%" + attendWhitelistQuery.getEmpInfoId() + "%");
        }
        if (attendWhitelistQuery.getEmpInfoName() != null) {
            example.and().andLike("empInfoName", "%" + attendWhitelistQuery.getEmpInfoName() + "%");
        }
        if (attendWhitelistQuery.getAttendPlanId() != null) {
            example.and().andLike("attendPlanId", "%" + attendWhitelistQuery.getAttendPlanId() + "%");
        }
        if (attendWhitelistQuery.getGmtCreateStart() != null &&
                attendWhitelistQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", attendWhitelistQuery.getGmtCreateStart(),
                    attendWhitelistQuery.getGmtCreateEnd());
        }
        if (attendWhitelistQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + attendWhitelistQuery.getCreateUserId() + "%");
        }
        if (attendWhitelistQuery.getGmtModifiedStart() != null &&
                attendWhitelistQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", attendWhitelistQuery.getGmtModifiedStart(),
                    attendWhitelistQuery.getGmtModifiedEnd());
        }
        if (attendWhitelistQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + attendWhitelistQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,attendWhitelistQuery.getOrders());
        return example;
    }
}




